<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
  // 按需引入 引入 ECharts 主模块
  // var echarts = require('echarts/lib/echarts')
  var echarts = require('echarts');
  // 引入柱状图
  require('echarts/lib/chart/line')
  // 引入提示框和标题组件
  require('echarts/lib/component/tooltip')
  require('echarts/lib/component/title')
  require('echarts/lib/component/legend')
  import chartResize from './mixins/chart-resize'

  export default {
    mixins:[chartResize],
    name:'line-chart',
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '350px'
      },
      autoResize: {
        type: Boolean,
        default: true
      },
      // 数据对象属性 代表的意义
      // dataXa x轴配置的数据
      // y1Data y轴第一条数据
      // y2Data y轴第二条线的数据
      chartData: {
        type: Object,
        required: true
      },

    },
    data() {
      return {
        chart: null
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler(val) {
          this.setOptions(val)
        }
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        debugger
        this.chart = echarts.init(this.$el, 'macarons')
        this.setOptions(this.chartData)
      },
      setOptions({ y1Data, y2Data } = {}) {
        this.chart.setOption({
          xAxis: {
            type: 'category',
            data:this.chartData.dataXa,
            // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
            boundaryGap: false,
            axisTick: {
              show: false
            }
          },
          grid: {
            left: 10,
            right: 10,
            bottom: 20,
            top: 30,
            containLabel: true
          },

          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10]
          },
          yAxis: {
            axisTick: {
              show: false
            }
          },

          legend: {
            data: ['y1', 'y2']
          },
          series: [{
            name: 'y1',
            itemStyle: {
              normal: {
                color: '#FF005A',
                lineStyle: {
                  color: '#FF005A',
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data: y1Data,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
            {
              name: 'y2',
              smooth: true,
              type: 'line',
              itemStyle: {
                normal: {
                  color: '#3888fa',
                  lineStyle: {
                    color: '#3888fa',
                    width: 2
                  },
                  areaStyle: {
                    color: '#f3f8ff'
                  }
                }
              },
              data: y2Data,
              animationDuration: 2800,
              animationEasing: 'quadraticOut'
            }]
        })
      }
    }
  }
</script>
